package org.road0329.tools.proto_parse_tool.ems.qgdw3761.utils;

import org.road0329.tools.proto_parse_tool.chargingpile.MsgDcsResult;
import org.road0329.tools.proto_parse_tool.ems.dataobject.gw.GwItemData;
import org.road0329.tools.proto_parse_tool.utils.ProtocolGW_09Util;
import org.road0329.tools.proto_parse_tool.utils.ProtocolUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-09-13.
 */
@Component
public class Gw09TransmitUtil {

    private final static short EVENT_ITEM_OVERLOAD = 130;//过载事件

    private static Logger logger = LoggerFactory.getLogger(Gw09TransmitUtil.class);

    public static MsgDcsResult parseTransmitData(byte[] msg, int from, boolean acd) {
        int index = from;
        int fixlen = (msg[index] & 0x80) == 0 ? 2 : 8;
        if (acd) {
            fixlen += 2;
        }
        index++;
        MsgDcsResult gwResult = new MsgDcsResult();
        List<GwItemData> gwItemList = new ArrayList<GwItemData>();
        String rtuAddr = getRtuAddr(msg);

        while ((index + fixlen) < msg.length) {
            Integer pn = ProtocolGW_09Util.getPnFromDA(msg[index + 1], msg[index]);
            int fn = ProtocolGW_09Util.getFnFromDT(msg[index + 3], msg[index + 2]);
            index += 4;
            GwItemData item = new GwItemData();
            item.setFn(fn);
            item.setPn(pn);
            if (fn == 1) {//透明转发
                int transLength = ProtocolUtils.byteToShort(msg[index + 2], msg[index + 1]);
                int remainLength = (msg.length - (index + 3));
                if(transLength < 0 || transLength > remainLength) {
                    transLength = remainLength;
                }
                if (transLength != 0) {
                    index += 3;
                    int transIndex = 0;
                    for (int i = 0; i < transLength; i++) {
                        if (ProtocolUtils.Unsignd(msg[index + i]) == 0x68) {
                            transIndex = i;
                            break;
                        }
                    }
                    if(transIndex > 5 || (transLength - transIndex) < 7) { //modbus没有68 帧头标识
                        transIndex = 0;
                    }
                    byte[] newBytes = new byte[transLength - transIndex];
                    //去掉EFEFEF
                    for (int i = 0; i < newBytes.length; i++) {
                        newBytes[i] = msg[index + i + transIndex];
                    }
                    item.setData(ProtocolUtils.printMsg(0, newBytes.length, newBytes));
                }
                index += transLength;
            }
            gwItemList.add(item);
        }
        gwResult.setData(gwItemList);
        gwResult.setSuccess(true);
        gwResult.setMessage("成功");
        return gwResult;
    }


    private static String getRtuAddr(byte[] msg) {
        return String.format("%02X%02X%02X%02X", msg[8], msg[7], msg[10], msg[9]);
    }
}
